package com.mingxun.project.base;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.mingxun.project.R;
import com.mingxun.project.listener.LifeCycleListener;
import com.mingxun.project.manager.ActivityStackManager;
import com.mingxun.project.util.LoadingUtils;
import com.utilmodule.NetWorkUtil;
import com.utilmodule.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * class name：
 *
 * @author liuhongyuan.
 * @date 2017/12/19
 */

public abstract class BaseActivity extends AppCompatActivity implements BaseView {
    @BindView(R.id.tv_title)
    TextView tvTitle;

    protected abstract int getLayoutResource();

    protected abstract void onInitView(Bundle savedInstanceState);

    protected abstract void onInitData();

    protected abstract boolean isBindEventBusHere();

    protected abstract void initPresenter();

    private KProgressHUD hud;

    public Activity mActivity;

    private FrameLayout contentLayout;

    public RelativeLayout actionBarLayout;

    private View loadingLayout, emptyLayout, errorLayout;

    Unbinder unbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mActivity = this;
        ActivityStackManager.getManager().push(this);
        View successView = View.inflate(this, getLayoutResource(), null);
        setContentView(R.layout.activity_base_layout);

        actionBarLayout = (RelativeLayout) findViewById(R.id.actionbar_layout);
        loadingLayout = findViewById(R.id.loading_layout);
        emptyLayout = findViewById(R.id.empty_layout);
        errorLayout = findViewById(R.id.error_layout);

        contentLayout = (FrameLayout) findViewById(R.id.content_layout);
        contentLayout.addView(successView);

        unbinder = ButterKnife.bind(this);
        initPresenter();
        if (isBindEventBusHere()) {
            EventBus.getDefault().register(this);
        }
        onInitView(savedInstanceState);
        onInitData();
    }

    public void setTitle(String title) {
        if(tvTitle != null) {
            tvTitle.setText(title);
        }
    }

    public void setActionBarVisb(int visiblity){
        actionBarLayout.setVisibility(visiblity);
    }

    @Override
    public void showMessage(String message) {
        showMessage("提示", message);
    }

    @Override
    public void showMessage(String title, String message) {
        if (TextUtils.isEmpty(title)) {
            message = title;
        }
        AlertDialog dialog = new AlertDialog.Builder(this).setTitle(title).setMessage(message).setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        }).setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        }).create();
        dialog.show();
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.BLUE);
        dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(Color.BLACK);
    }

    public View getsuccessView() {
        View successView = View.inflate(this, getLayoutResource(), null);
        return successView;
    }

    @Override
    public void showToast(String msg) {
        ToastUtils.show(this,msg,1000);
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        if (isBindEventBusHere()) {
            EventBus.getDefault().unregister(this);
        }
        if (hud != null) {
            hud.dismiss();
        }
        ActivityStackManager.getManager().remove(this);
    }

    @Override
    public void showDialog(String message) {
        if (hud != null) {
            hud.show();
            return;
        }
        hud = KProgressHUD.create(this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel(message)
//                .setDetailsLabel(message)
                .setCancellable(true)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f)
                .show();
    }

    @Override
    public void hideDialog() {
        if (hud != null) {
            hud.dismiss();
        }
    }

    @Override
    public void initRecyclerView(RecyclerView recyclerView, int orientation) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        layoutManager.setOrientation(orientation);
        recyclerView.setNestedScrollingEnabled(false);
    }

    @Override
    public void initGridRecyclerView(RecyclerView recyclerView, int orientation, int column) {
        GridLayoutManager layoutManager = new GridLayoutManager(this, column);
        recyclerView.setLayoutManager(layoutManager);
        layoutManager.setOrientation(orientation);
        recyclerView.setNestedScrollingEnabled(false);
    }

    public void onLoadingView(int state) {
        switch (state){
            case LoadingUtils.STATE_UNLOADED:
            case LoadingUtils.STATE_LOADING:
                contentLayout.setVisibility(View.GONE);
                loadingLayout.setVisibility(View.VISIBLE);
                emptyLayout.setVisibility(View.GONE);
                errorLayout.setVisibility(View.GONE);
                break;
            case LoadingUtils.STATE_ERROR:
                contentLayout.setVisibility(View.GONE);
                loadingLayout.setVisibility(View.GONE);
                emptyLayout.setVisibility(View.GONE);
                errorLayout.setVisibility(View.VISIBLE);
                break;
            case LoadingUtils.STATE_EMPTY:
                contentLayout.setVisibility(View.GONE);
                loadingLayout.setVisibility(View.GONE);
                emptyLayout.setVisibility(View.VISIBLE);
                errorLayout.setVisibility(View.GONE);
                break;
            case LoadingUtils.STATE_SUCCEED:
                contentLayout.setVisibility(View.VISIBLE);
                loadingLayout.setVisibility(View.GONE);
                emptyLayout.setVisibility(View.GONE);
                errorLayout.setVisibility(View.GONE);
                break;
        }
    }

    /**
     * 回调函数
     */
    public LifeCycleListener mListener;

    public void setOnLifeCycleListener(LifeCycleListener listener) {
        mListener = listener;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (NetWorkUtil.getNetWorkType(this) == NetWorkUtil.NETWORKTYPE_WIFI
                || NetWorkUtil.getNetWorkType(this) == NetWorkUtil.NETWORKTYPE_3G) {
            onLoadingView(LoadingUtils.STATE_SUCCEED);
        } else if (NetWorkUtil.getNetWorkType(this) == NetWorkUtil.NETWORKTYPE_INVALID) {
            onLoadingView(LoadingUtils.STATE_ERROR);
        } else {
            onLoadingView(LoadingUtils.STATE_LOADING);
        }
    }


}
